package guyubao.com.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guyubao.com.GreenLoong.GreenLoong;
import guyubao.com.entity.dto.*;
import guyubao.com.entity.vo.response.JDAccountInfoVo;
import guyubao.com.entity.vo.response.JDAccountVo;
import guyubao.com.entity.vo.response.JDCertificateVo;
import guyubao.com.mapper.JDAccountInfoMapper;
import guyubao.com.mapper.LoginMapperUserMapper;
import guyubao.com.service.JDCertificateService;
import guyubao.com.service.LoginMapperUserService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoginMapperUserServiceImpl extends ServiceImpl<LoginMapperUserMapper, LoginMapperUserDto> implements LoginMapperUserService {
    @Resource
    LoginMapperUserMapper loginMapperUserMapper;
    @Resource
    JDAccountInfoMapper jdAccountInfoMapper;
    @Resource
    JDCertificateService jdCertificateService;
    
    @Resource
    GreenLoong greenLoong;

    /***
     * 添加
     */
    @Override
    public String addLoginMapperUser(String id, String uId) {
        return this.save(new LoginMapperUserDto(id,uId)) ? null : "使用户("+id+")绑定账号("+uId+")失败,请联系管理人员!";
    }

    /***
     * 获取用户下所有uid
     */
    @Override
    public List<String> getUIds(String id) {
        return id!=null&&!id.isEmpty()? loginMapperUserMapper.getUIdById(id):loginMapperUserMapper.getUId();
    }
    /***
     * 根据uid获取账号信息 uid 账号 昵称 备注
     */
    @Override
    public JDAccountDto getJDAccount(String uid) {
        return jdAccountInfoMapper.getJDAccount(uid);
    }

    /**
     * 获取用户下所有账号信息 uid 账号 昵称 备注
     */
    @Override
    public List<JDAccountVo> getJDAccounts(String id) {
        List<String> uIds = this.getUIds(id);
        ArrayList<JDAccountVo> objects = new ArrayList<>();
        uIds.forEach((uid)->{
            JDAccountDto account = this.getJDAccount(uid);
            JDCertificateVo proof = jdCertificateService.getJDCertificateProofBase(uid);
            objects.add(new JDAccountVo(account.getUId(),
                    account.getPt_pin(),
                    account.getNickname(),
                    account.getComment(),
                    proof.getCount(),
                    proof.getState()==1?"正常":"异常"));
        });
        return objects;
    }
    /**
     * 获取用户下所有账号信息 uid 账号 昵称 备注 返回分页信息
     * 如果大于int范围页数会出现问题
     */
    @Override
    public Page<JDAccountVo> getJDAccountsPage(String id, long size, long current) {
        // 获得该用户下所有账号uid(如果没有用户就获取所有账号)
        List<JDAccountVo> jdAccounts;
        if (id==null||id.isEmpty()) jdAccounts = this.getJDAccounts(null);
        else jdAccounts = this.getJDAccounts(id);
        // 确认返回的分页
        Page<JDAccountVo> page = new Page<>();
        // 页条数不能小于1
        if (size<1) page.setSize(1);
        else page.setSize(size);
        // 当前页不能小于1
        if (current<1) page.setCurrent(1);
        else page.setCurrent(current);
        // 设置总条数也就是账号的数量
        int dataSize;
        if (jdAccounts==null){
            page.setTotal(0);dataSize=0;
        } else {
            page.setTotal(jdAccounts.size());
            dataSize=jdAccounts.size();
        }
        // 如果页条数*当前页》总条数
        if(size*current>dataSize){
            // 如果不完全大于有几条剩余
            if ((current-1)*size<dataSize)
                page.setRecords(jdAccounts.subList((int)((current-1)*size),dataSize));
            else page.setRecords(new ArrayList<>());
        }else{
            page.setRecords(jdAccounts.subList((int)((current-1)*size),(int)(current*size)));
        }
        page.setPages((long)Math.ceil((double) dataSize/size));
        return page;
    }


    /***
     * 根据uid获取该账号所有信息
     */
    @Override
    public JDAccountInfoVo getJDAccountInfos(String uid) {
        return jdAccountInfoMapper.getJDAccountInfoVo(uid);
    }



    /***
     * 根据uid获取该账号简略信息
     */
    @Override
    public JDAccountInfoDto getJDAccountInfo(String uid) {
        return jdAccountInfoMapper.getJDAccountInfo(uid);
    }
    
    /***
     * 根据uid删除绑定
     */
    @Override
    public String delJDAccount(String uid) {
        Integer i = loginMapperUserMapper.delLoginMapperUserByUId(uid);
        if (i!=null) {
            return "删除失败!请联系管理人员";
        }
        return null;
    }

    @Override
    public String getIdByUId(String uid) {
        return loginMapperUserMapper.getIdByUId(uid);
    }
}
